package presenter

import (
	"github.com/emicklei/go-restful/v3"
	"github.com/rs/zerolog/log"
)

type CommonRes struct {
	Response *restful.Response      `json:"-"`
	Err      error                  `json:"-"`
	Code     int                    `json:"code"`
	MsgRes   string                 `json:"message"`
	ToLogMsg map[string]interface{} `json:"-"`
	Data     interface{}            `json:"data"`
}

type CloudCommonRes struct {
	Response *restful.Response      `json:"-"`
	Err      error                  `json:"-"`
	Code     string                 `json:"code"`
	MsgRes   string                 `json:"msg"`
	ToLogMsg map[string]interface{} `json:"-"`
	Data     interface{}            `json:"data"`
}

func NewCommonRes(res *restful.Response) *CommonRes {
	commRes := new(CommonRes)
	commRes.Response = res
	commRes.Err = nil
	commRes.Code = CodeSuccess
	commRes.MsgRes = MsgSuccess
	commRes.ToLogMsg = make(map[string]interface{}, 0)
	commRes.Data = nil
	return commRes
}

func NewCloudCommonRes(res *restful.Response) *CloudCommonRes {
	commRes := new(CloudCommonRes)
	commRes.Response = res
	commRes.Err = nil
	commRes.Code = "0"
	commRes.MsgRes = "success"
	commRes.ToLogMsg = make(map[string]interface{}, 0)
	commRes.Data = nil
	return commRes
}

func (s *CommonRes) ToApiOut(response *restful.Response, code int, msg string, data interface{}) {
	s.Code = code
	s.MsgRes = msg
	s.Data = struct {
	}{}
	if data != nil {
		s.Data = data
	}
	_ = response.WriteAsJson(s)
	return
}

func (s *CloudCommonRes) ToApiOut(response *restful.Response, code string, msg string, data interface{}) {
	s.Code = code
	s.MsgRes = msg
	s.Data = struct {
	}{}
	if data != nil {
		s.Data = data
	}
	_ = response.WriteAsJson(s)
	return
}

/*
func (s *CommonRes) ToLog(err error, args ...interface{}) {
	msgList := make([]interface{}, 0)
	for _, v := range args {
		msgList = append(msgList, v)
	}
	msgBytes, _ := json.Marshal(msgList)
	logs.Error().Err(err).Msg(string(msgBytes))
	return
}*/

func (s *CommonRes) ToLog(err error, args ...interface{}) {

	isKey := true
	logEvent := log.Error().Err(err)
	key := ""
	for _, v := range args {
		if isKey {
			key = v.(string)
			isKey = false
		} else {
			logEvent = logEvent.Interface(key, v)
			isKey = true
		}
	}
	logEvent.Send()
	return
}
func (s *CloudCommonRes) ToLog(err error, args ...interface{}) {

	isKey := true
	logEvent := log.Error().Err(err)
	key := ""
	for _, v := range args {
		if isKey {
			key = v.(string)
			isKey = false
		} else {
			logEvent = logEvent.Interface(key, v)
			isKey = true
		}
	}
	logEvent.Send()
	return
}
func (s *CommonRes) Out() {
	if s.Response != nil {
		s.ToApiOut(s.Response, s.Code, s.MsgRes, s.Data)
	}
	if s.Err != nil {
		argsList := make([]interface{}, 0)
		argsList = append(argsList, "code")
		argsList = append(argsList, s.Code)
		for k, v := range s.ToLogMsg {
			argsList = append(argsList, k)
			argsList = append(argsList, v)
		}
		s.ToLog(s.Err, argsList...)
	}
}
func (s *CloudCommonRes) Out() {
	if s.Response != nil {
		s.ToApiOut(s.Response, s.Code, s.MsgRes, s.Data)
	}
	if s.Err != nil {
		argsList := make([]interface{}, 0)
		argsList = append(argsList, "code")
		argsList = append(argsList, s.Code)
		for k, v := range s.ToLogMsg {
			argsList = append(argsList, k)
			argsList = append(argsList, v)
		}
		s.ToLog(s.Err, argsList...)
	}
}

